<?php
require_once dirname(__FILE__)."/base_model.php";
class workers extends base_model {

	const WORKERS_TABLE = "table_workers";

	public function __construct() {
		parent::__construct();
	}

	//工作组成员
	public function getAllWorkersByGroupId($group_id) {
		$this->db->select('worker_id, worker_name, worker_groupid');
		$this->db->from(self::WORKERS_TABLE);
		$this->db->where('worker_groupid', $group_id);
		$this->db->order_by('is_charger', 'desc');
		return $this->db->get()->result_array();
	}
    /**
     * 根据用户名查询工作人员
     * @param string $pt
     * @return array
     */
	public function getWorkerByName($username) {
		$this->db->from(self::WORKERS_TABLE);
		$this->db->where('worker_name', $username);
		return $this->db->get()->row_array();
	}
	/**
	 * 增加工作人员
	 * @param string $username
	 * @param int $groupid
	 * @param array $row
	 */
	public function addWorker($username,$groupid=0,$row = array())
	{
		$row = array_merge(array(
			'worker_name'=>$username,
			'worker_groupid'=>$groupid
		), $row);
	    if (!$this->db->insert(self::WORKERS_TABLE, $row)) {
	        $this->logger->err("add worker failure,worker_name-{$username}, worker_groupid-{$groupid}.");
	        return false;
	    }
	    return $this->db->insert_id();
	}
	/**
	 * 更新工作人员信息
	 * @param int $workerId 事件号
	 * @param array $row
	 * @return boolean
	 */
    public function modifyWorkerById($workerId, $row=array())
	{
	    $tmp = $this->getWorkerById($workerId);
	    if(!$tmp) {
	        $this->_setError(-1, "the worker({$workerId}) not exists.");
	        return false;
	    }
	    if (!$this->db->update(self::WORKERS_TABLE, $row, array('worker_id'=>$workerId))) {
	    	$this->logger->err("update worker failure,worker_id:{$workerId}.");
	        return false;
	    }
	    return true;
	}
	/**
	 * 增加工作人员
	 * @param int $workerId
	 * @return array
	 */
	public function getWorkerById($workerId)
	{
		$this->db->from(self::WORKERS_TABLE);
		$this->db->where('worker_id', $workerId);
		return $this->db->get()->row_array();
	}
	/**
	 * 删除工作人员
	 * @param int $workerId
	 * @return boolean
	 */
    public function deleteWorkerById($workerId)
	{
	    $tmp = $this->getWorkerById($workerId);
	    if(!$tmp) {
	        $this->_setError(-1, "the worker({$workerId}) not exists.");
	        return false;
	    }
	    if (!$this->db->delete(self::WORKERS_TABLE, array('worker_id'=>$workerId))) {
	    	$this->logger->err("delete worker failure,worker:{$workerId}.");
	        return false;
	    }
	    return true;
	}
	/**
	 * 获取管理员列表
	 * @param array $where
	 * @param int $offset
	 * @param int $count
	 * @param string $sort
	 * @return array
	 */
	public function getWorkerList($where, $offset=0, $count=null, $orderby=null, $sort=null)
	{
	   $this->db->from(self::WORKERS_TABLE);
	   foreach($where as $k=>$row) {
	       if (is_array($row)) {
	       	    $this->db->where_in($k, $row);
	       	    unset($where[$k]);
	       }
	   }
	   $this->db->where($where);
	   if ($count) {
	       $this->db->limit($count);
	   }
	   if ($offset) {
	       $this->db->offset($offset);
	   }
	   if ($orderby) {
	       if ($sort) {
	       	   $this->db->orderby($orderby, $sort);
	       } else {
	           $this->db->orderby($orderby);
	       }
	   }
	   return $this->db->get()->result_array();
	}
	/**
	 * 获取管理员记录条数
	 * @param array $where
	 * @return int
	 */
    public function getWorkerCount($where)
	{
	   $this->db->from(self::WORKERS_TABLE);

	   $this->db->select("count(*) as c");
	   foreach($where as $k=>$row) {
	       if (is_array($row)) {
	       	    $this->db->where_in($k, $row);
	       	    unset($where[$k]);
	       }
	   }
	   $this->db->where($where);
	   $row = $this->db->get()->row_array();
	   return intval($row['c']);
	}
}